package com.SouthernWall_404.AgeDust.Common.API.Capability;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlayerPersonalityProvider implements ICapabilityProvider, INBTSerializable {

    private PlayerPersonality playerPersonality;

    public static final Capability<PlayerPersonality>PLAYER_PERSONALITY_CAPABILITY= CapabilityManager.get(new CapabilityToken<>(){});

    private final LazyOptional<PlayerPersonality> lazyOptional=LazyOptional.of(()->this.playerPersonality);

    public PlayerPersonalityProvider() {
        this.playerPersonality = new PlayerPersonality();
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        return getCapability(cap);
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap) {
        if(cap==PLAYER_PERSONALITY_CAPABILITY)
        {
            return lazyOptional.cast();
        }
        else
        {
            return LazyOptional.empty();
        }
    }

    @Override
    public Tag serializeNBT() {
        var nbt=new CompoundTag();
        playerPersonality.saveNBT(nbt);
        return nbt;
    }

    @Override
    public void deserializeNBT(Tag nbt) {
        playerPersonality.readNBT((CompoundTag) nbt);
    }
}
